package com.doctor.windflower_doctor.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.SystemApplication;
import com.doctor.windflower_doctor.actionBeen.QuestionAction;
import com.doctor.windflower_doctor.db.SharedPreferencesDB;
import com.doctor.windflower_doctor.entity.QuestionBeen;
import com.doctor.windflower_doctor.http.callback.HttpRequestCallBack;
import com.doctor.windflower_doctor.http.parser.impl.JsonParser;
import com.doctor.windflower_doctor.http.request.HttpRequest;
import com.doctor.windflower_doctor.http.request.HttpRequestParams;
import com.doctor.windflower_doctor.http.response.HttpResponseInfo;
import com.doctor.windflower_doctor.util.Constent;
import com.doctor.windflower_doctor.util.StringUtil;
import com.doctor.windflower_doctor.view.RoundedImageView;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

public class ReportActivity extends BaseActivity {

	private Button answer;
	private ImageButton back;
	private TextView name;
	private Button button_report;
	private QuestionBeen questionBeen;
	private RoundedImageView imageView;
	private TextView textView;
	private DisplayImageOptions options;

	@Override
	protected void getSaveData(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void processLogic() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		answer.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				postTime(questionBeen.questionId);

			}
		});
		button_report.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ReportActivity.this, PostReportActivity.class);
				intent.putExtra("questionId", questionBeen.questionId);
				startActivity(intent);
			}
		});
		imageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ReportActivity.this, SickDetailActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("userId", questionBeen.userId);
				intent.putExtras(bundle);
				startActivity(intent);
			}
		});
	}

	@Override
	protected void findViews() {
		// TODO Auto-generated method stub

		button_report = (Button) findViewById(R.id.button_report);
		options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.loading_img).showImageOnFail(R.drawable.loading_img)
				.showImageForEmptyUri(R.drawable.loading_img).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.ARGB_8888).build();
		Intent intent = getIntent();
		questionBeen = (QuestionBeen) intent.getSerializableExtra("itme");
		imageView = (RoundedImageView) findViewById(R.id.iv_user_image);
		textView = (TextView) findViewById(R.id.tv_content);
		SystemApplication.getInstance().mImageLoad1.displayImage(questionBeen.imgUrl, imageView, options);
		textView.setText(questionBeen.text);
		answer = (Button) findViewById(R.id.button_answer);
		back = (ImageButton) findViewById(R.id.imageButton_back);
		name = (TextView) findViewById(R.id.textView_name);
		if(questionBeen.nick!=null&&questionBeen.nick.equals("")){
			name.setText("用户"
					+ questionBeen.userId.subSequence(questionBeen.userId.length() - 6, questionBeen.userId.length()));
		}else if(questionBeen.nick==null){
			name.setText("用户"
					+ questionBeen.userId.subSequence(questionBeen.userId.length() - 6, questionBeen.userId.length()));
		
		}else{
			name.setText(questionBeen.nick);
		}

		

	}

	@Override
	public int getResLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.reportactivity;
	}

	private void postTime(String questionId) {
		String url = Constent.URL + "ask/doctor/answerQuestion";
		HttpMethod method = HttpMethod.POST;
		HttpRequestCallBack<QuestionAction> requestCallBack = new HttpRequestCallBack<QuestionAction>(new JsonParser(), QuestionAction.class) {

			@Override
			protected void handleResult(HttpResponseInfo<QuestionAction> responseInfo) {
				if (responseInfo.success != null) {
					if (responseInfo.success.success) {
						Intent intent = new Intent(MainActivity.ACTION_FRAGMENT_MYQUESTIONFRAGMENT_Q);
						sendBroadcast(intent);
						finish();
					} else {
						Toast.makeText(ReportActivity.this, responseInfo.success.message, 1).show();
						finish();
					}
				}

			}

			@Override
			protected void handleError(HttpException e, String msg) {
				// TODO Auto-generated method stub
				Toast.makeText(ReportActivity.this, "请检查网络状态", 1).show();
			}
		};
		HttpRequestParams params = new HttpRequestParams(method, url, requestCallBack);
		params.addBodyParameter("doctorId", SharedPreferencesDB.getInstance(ReportActivity.this).getDocotorId());
		params.addBodyParameter("questionId", questionId);
		HttpRequest.send(params);
	}
}
